package com.bluse.handler;


import com.bluse.exception.MyErrorException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @Describe: TODO
 * @Author: bluse
 * @Date: 2018/12/25 18:15
 **/

@ControllerAdvice
public class ExceptionHanlder {
    
    private static final Logger log = LoggerFactory.getLogger(ExceptionHandler.class);

    @ExceptionHandler({Exception.class, NullPointerException.class})
    @ResponseBody
    public Object jsonErrorHandler(HttpServletRequest request, Exception e) {
        MyErrorException myExp = (MyErrorException) e;
        Map<String, Object> expMsg = new LinkedHashMap<>();
        expMsg.put("code", myExp.getCode());
        expMsg.put("msg", myExp.getMessage());
        log.error("Request error:{}", expMsg.toString());
        return expMsg;
    }


}